package com.fr.web.core;

import com.fr.base.FRContext;
import com.fr.base.FRFont;
import com.fr.base.Inter;
import com.fr.base.Painter;
import com.fr.base.Style;
import com.fr.base.background.Background;
import com.fr.base.background.ColorBackground;
import com.fr.base.background.ImageBackground;
import com.fr.base.background.TextureBackground;
import com.fr.base.core.GraphHelper;
import com.fr.base.core.html.Tag;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.report.CellElement;
import com.fr.report.DefaultCellElement;
import com.fr.report.WorkSheet;
import com.fr.report.cellElement.CellGUIAttr;
import com.fr.report.cellElement.core.CellUtils;
import com.fr.report.core.DynamicValueList;
import com.fr.report.core.ReportHelper;
import com.fr.report.io.core.IOHelper;
import com.fr.report.js.JavaScript;
import com.fr.report.js.NameJavaScript;
import com.fr.report.js.NameJavaScriptGroup;
import com.fr.report.parameter.Parameter;
import com.fr.report.web.ui.impl.form.FormResetButton;
import com.fr.report.web.ui.impl.form.FormSubmitButton;
import com.fr.util.Utils;
import com.fr.web.Browser;
import com.fr.web.Repository;
import com.fr.web.core.chwriter.FormCellWriter;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Rectangle2D.Double;
import java.awt.image.BufferedImage;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

public class HTMLWriterUtils
{
  public static void writeBackground(Tag paramTag, Background paramBackground, BackgroundDimension paramBackgroundDimension, boolean paramBoolean1, Repository paramRepository, boolean paramBoolean2)
  {
    if (paramBackground instanceof ColorBackground)
    {
      paramTag.css("background-color", Utils.javaColorToCSSColor(((ColorBackground)paramBackground).getColor()));
    }
    else if (paramBackground != null)
    {
      int i = paramBackgroundDimension.getWidth();
      int j = paramBackgroundDimension.getHeight();
      if ((i == 0) || (j == 0))
        return;
      BufferedImage localBufferedImage = GraphHelper.createBufferedImage(i, j, 2);
      Graphics2D localGraphics2D = localBufferedImage.createGraphics();
      paramBackground.paint(localGraphics2D, new Rectangle2D.Double(0.0D, 0.0D, i, j));
      localBufferedImage.flush();
      localGraphics2D.dispose();
      if (!(paramBoolean1))
        paramTag.css("background-repeat", "no-repeat");
      paramRepository.writeTagBackground(paramTag, localBufferedImage, paramBoolean2);
    }
  }

  public static void writeBackground(Tag paramTag, Background paramBackground, BackgroundDimension paramBackgroundDimension, boolean paramBoolean, Repository paramRepository)
  {
    writeBackground(paramTag, paramBackground, paramBackgroundDimension, paramBoolean, paramRepository, true);
  }

  public static JSONObject jsonBackground(Background paramBackground, Repository paramRepository)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    if (paramBackground instanceof ColorBackground)
    {
      localStringBuffer.append(Utils.javaColorToCSSColor(((ColorBackground)paramBackground).getColor()));
    }
    else if (paramBackground instanceof ImageBackground)
    {
      ImageBackground localImageBackground = (ImageBackground)paramBackground;
      localStringBuffer.append("url(").append(paramRepository.checkoutImage(localImageBackground.getDefaultImage())).append(") ");
      if (localImageBackground.getLayout() == 3)
        localStringBuffer.append("no-repeat");
      else if (localImageBackground.getLayout() == 1)
        localStringBuffer.append("no-repeat center");
      else if (localImageBackground.getLayout() == 0)
        localStringBuffer.append("repeat");
      else if (localImageBackground.getLayout() == 2)
        localStringBuffer.append("no-repeat center");
    }
    else if (paramBackground != null)
    {
      int i;
      int j;
      if (paramBackground instanceof TextureBackground)
      {
        i = 174;
        j = 126;
      }
      else
      {
        i = 160;
        j = 160;
      }
      BufferedImage localBufferedImage = GraphHelper.createBufferedImage(i, j, 2);
      Graphics2D localGraphics2D = localBufferedImage.createGraphics();
      paramBackground.paint(localGraphics2D, new Rectangle2D.Double(0.0D, 0.0D, i, j));
      localBufferedImage.flush();
      localGraphics2D.dispose();
      localStringBuffer.append("url(").append(paramRepository.checkoutImage(localBufferedImage)).append(") ").append("repeat ");
    }
    JSONObject localJSONObject = new JSONObject();
    if (localStringBuffer.length() > 0)
      try
      {
        localJSONObject.put("background", localStringBuffer.toString());
      }
      catch (JSONException localJSONException)
      {
        FRContext.getLogger().log(Level.WARNING, localJSONException.getMessage(), localJSONException);
      }
    return localJSONObject;
  }

  public static String writeJSLinkContent(NameJavaScriptGroup paramNameJavaScriptGroup, Repository paramRepository)
    throws JSONException
  {
    JSONArray localJSONArray = new JSONArray();
    for (int i = 0; i < paramNameJavaScriptGroup.size(); ++i)
    {
      JSONObject localJSONObject = new JSONObject();
      localJSONObject.put("data", paramNameJavaScriptGroup.getNameHyperlink(i).getJavaScript().createJS(paramRepository).replaceAll("\n", ""));
      localJSONObject.put("name", paramNameJavaScriptGroup.getNameHyperlink(i).getName());
      localJSONArray.put(localJSONObject);
    }
    return localJSONArray.toString();
  }

  public static void value2PaintableTag(Object paramObject, Style paramStyle, int paramInt1, int paramInt2, Tag paramTag, Repository paramRepository, boolean paramBoolean)
  {
    Image localImage;
    if ((paramObject instanceof Image) && (paramStyle != null) && (paramStyle.getImageLayout() == 3))
    {
      localImage = (Image)paramObject;
      paramTag.sub(paramRepository.checkoutImageTag(localImage, localImage.getWidth(null), localImage.getHeight(null)));
    }
    else if (paramObject instanceof Painter)
    {
      if (paramRepository.getBrowser().shouldModifyWidth4PainterAccordingToBorderWidth())
        paramInt1 -= (IOHelper.getBorderWidth(paramStyle.getBorderLeft()) + IOHelper.getBorderWidth(paramStyle.getBorderRight()) + 1) / 2;
      if (paramBoolean)
        paramTag.sub(paramRepository.checkoutImageTag(CellUtils.value2Image((Painter)paramObject, paramStyle, paramInt1, paramInt2), paramInt1, paramInt2));
      else
        ((Painter)paramObject).paintTag(paramRepository, paramInt1, paramInt2, paramStyle, paramTag);
    }
    else
    {
      localImage = CellUtils.value2ImageWithBackground(paramObject, paramStyle, paramInt1, paramInt2);
      paramTag.sub(paramRepository.checkoutImageTag(localImage, paramInt1, paramInt2));
    }
  }

  public static StringBuffer writeAlign(Style paramStyle)
  {
    StringBuffer localStringBuffer = new StringBuffer();
    int i = paramStyle.getHorizontalAlignment();
    if (i == 2)
      localStringBuffer.append("text-align:left;");
    else if (i == 0)
      localStringBuffer.append("text-align:center;");
    else if (i == 4)
      localStringBuffer.append("text-align:right;");
    int j = paramStyle.getVerticalAlignment();
    if (j == 1)
      localStringBuffer.append("vertical-align:top;");
    else if (j == 0)
      localStringBuffer.append("vertical-align:middle;");
    else if (j == 3)
      localStringBuffer.append("vertical-align:bottom;");
    return localStringBuffer;
  }

  public static void contentStyle2Css(Style paramStyle, Tag paramTag)
  {
    paramTag.cls(paramStyle.getContentClsCss());
    Map localMap = paramStyle.getContentStyleCssMap();
    if (localMap.size() > 0)
      paramTag.css(localMap);
  }

  public static void borderStyle2Css(Style paramStyle, Tag paramTag)
  {
    paramTag.cls(paramStyle.getBorderClsCss());
    Map localMap = paramStyle.getBorderStyleCssMap();
    if (localMap.size() > 0)
      paramTag.css(localMap);
  }

  public static void style_background2css(Style paramStyle, Tag paramTag, CellGUIAttr paramCellGUIAttr, BackgroundDimension paramBackgroundDimension, Repository paramRepository)
  {
    style_noBorder_background2css(paramStyle, paramTag, paramCellGUIAttr, paramBackgroundDimension, paramRepository);
    if ((paramCellGUIAttr == null) || (paramCellGUIAttr.isPreviewBorder()))
      borderStyle2Css(paramStyle, paramTag);
  }

  public static void style_noBorder_background2css(Style paramStyle, Tag paramTag, CellGUIAttr paramCellGUIAttr, BackgroundDimension paramBackgroundDimension, Repository paramRepository)
  {
    contentStyle2Css(paramStyle, paramTag);
    if ((paramCellGUIAttr == null) || (paramCellGUIAttr.isPreviewBackground()))
      writeBackground(paramTag, paramStyle.getBackground(), paramBackgroundDimension, false, paramRepository);
  }

  public static Tag writeRegForm(HttpServletRequest paramHttpServletRequest)
  {
    WorkSheet localWorkSheet = new WorkSheet();
    localWorkSheet.setHeader(0, null);
    localWorkSheet.setFooter(0, null);
    localWorkSheet.setColumnWidth(0, 100);
    Tag localTag = new Tag("form").attr("id", "fr_reg_form");
    Style localStyle = Style.DEFAULT_STYLE.deriveHorizontalAlignment(4);
    localStyle = localStyle.deriveFRFont(FRFont.getInstance("Dialog", 0, 13));
    DefaultCellElement localDefaultCellElement1 = new DefaultCellElement(0, 0, Inter.getLocText("Registration-Company_Name") + ":");
    localWorkSheet.addCellElement(localDefaultCellElement1);
    localDefaultCellElement1.setStyle(localStyle);
    DefaultCellElement localDefaultCellElement2 = new DefaultCellElement(1, 0, 4, 1, new Parameter("companyName"));
    localWorkSheet.addCellElement(localDefaultCellElement2);
    DefaultCellElement localDefaultCellElement3 = new DefaultCellElement(0, 1, Inter.getLocText("Registration-Project_Name") + ":");
    localWorkSheet.addCellElement(localDefaultCellElement3);
    localDefaultCellElement3.setStyle(localStyle);
    DefaultCellElement localDefaultCellElement4 = new DefaultCellElement(1, 1, 4, 1, new Parameter("projectName"));
    localWorkSheet.addCellElement(localDefaultCellElement4);
    DefaultCellElement localDefaultCellElement5 = new DefaultCellElement(0, 2, Inter.getLocText("Registration-Concurrency") + ":");
    localWorkSheet.addCellElement(localDefaultCellElement5);
    localDefaultCellElement5.setStyle(localStyle);
    Parameter localParameter = new Parameter("concurrency");
    DefaultCellElement localDefaultCellElement6 = new DefaultCellElement(1, 2, 4, 1, localParameter);
    localWorkSheet.addCellElement(localDefaultCellElement6);
    DefaultCellElement localDefaultCellElement7 = new DefaultCellElement(1, 3, new FormSubmitButton(Inter.getLocText("Registration-Use_Authorization_File")));
    localDefaultCellElement7.setStyle(Style.DEFAULT_STYLE.deriveHorizontalAlignment(0));
    localWorkSheet.addCellElement(localDefaultCellElement7);
    DefaultCellElement localDefaultCellElement8 = new DefaultCellElement(3, 3, new FormResetButton(Inter.getLocText("Reset")));
    localWorkSheet.addCellElement(localDefaultCellElement8);
    Repository localRepository = new Repository(paramHttpServletRequest, null);
    localTag.sub(HTMLWriter.getInstance().writeJSReport(localWorkSheet, 0, new FormCellWriter(localRepository, 0), localRepository));
    int i = ReportHelper.getColumnWidthList(localWorkSheet).getRangeValueFromZero(localWorkSheet.getColumnCount()) + 10;
    int j = ReportHelper.getRowHeightList(localWorkSheet).getRangeValueFromZero(localWorkSheet.getRowCount()) + 45;
    localTag.attr("title", Inter.getLocText("Registration")).attr("width", "" + i).attr("height", "" + j);
    return localTag;
  }
}